﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// 此类用于在“查找结果”树型框中表示找到的一个 Markdown 文档。
    /// </summary>
    public class FindDocumentTreeViewItem : TreeViewItem, IFindItem
    {
        /// <summary>
        /// [构造方法]用于在“查找结果”树型框中表示找到的一个 Markdown 文档。
        /// </summary>
        /// <param name="markdownFileFullName"></param>
        /// <param name="shortFileName">未存盘的文件需要短名才能辨别。</param>
        public FindDocumentTreeViewItem(string markdownFileFullName, string shortFileName, string customHeaderText = null)
        {
            this.BorderBrush = Brushes.Transparent;
            this.BorderThickness = new System.Windows.Thickness(1);

            if (string.IsNullOrEmpty(markdownFileFullName))
            {
                this.markdownFileFullName = shortFileName;
            }
            else
            {
                this.markdownFileFullName = markdownFileFullName;
            }

            this.Header = headerTextBlock = new TextBlock();
            if (string.IsNullOrWhiteSpace(customHeaderText) == false)
            {
                headerTextBlock.Text = customHeaderText;
            }
            else
            {
                headerTextBlock.Text = this.ShortName;
            }

            this.ToolTip = this.markdownFileFullName;
        }

        private string markdownFileFullName;
        /// <summary>
        /// Markdown 文件完整路径。
        /// </summary>
        public string MarkdownFileFullName
        {
            get { return markdownFileFullName; }
            set
            {
                markdownFileFullName = value;
            }
        }

        /// <summary>
        /// 根据完整路径取其短名。
        /// </summary>
        public string ShortName
        {
            get
            {
                if (System.IO.File.Exists(this.markdownFileFullName) == false)
                {
                    if (string.IsNullOrEmpty(this.markdownFileFullName))
                    {
                        return "文件不存在或未存盘！";
                    }
                    else return this.markdownFileFullName;//这是考虑到未存盘的文件。
                }

                var fileInfo = new System.IO.FileInfo(this.markdownFileFullName);
                return fileInfo.Name;
            }
        }

        private TextBlock headerTextBlock;
        /// <summary>
        /// 标头文本块。
        /// </summary>
        public TextBlock HeaderTextBlock
        {
            get { return this.headerTextBlock; }
        }

        private Brush foregroundOfText = Brushes.Black;
        /// <summary>
        /// 文本前景色。用于树型框失焦时保留边框。
        /// </summary>
        public Brush ForegroundOfText { get { return foregroundOfText; } }
    }
}
